package com.jwthhealth_pub.old.sportfitness.presenter;

import android.util.Log;

import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.sportfitness.module.SprotOverModule;
import com.jwthhealth_pub.old.sportfitness.view.SportCourseTrainActivity;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/9.
 */

public class ISportCourseTrainPresenterCompl implements ISportCourseTrainPresenter {

    private SportCourseTrainActivity activity;

    public ISportCourseTrainPresenterCompl(SportCourseTrainActivity activity) {
        this.activity = activity;
    }

    @Override
    public void unZip(String unZipfile, String destFile) {
        BufferedInputStream bi;
        ZipFile zipFile = null;
        try {
            File f = new File(unZipfile);
            zipFile = new ZipFile(f, "GBK");
//            zipFile = new ZipFile(f, "UTF-8");

            @SuppressWarnings("rawtypes")
            Enumeration e = zipFile.getEntries();
            while (e.hasMoreElements()) {
                ZipEntry ze2 = (ZipEntry) e.nextElement();
                String entryName = ze2.getName();
                String path = destFile + "/" + entryName;
                if (ze2.isDirectory()) {
                    System.out.println("正在创建解压目录 - " + entryName);
                    File decompressDirFile = new File(path);
                    if (!decompressDirFile.exists()) {
                        decompressDirFile.mkdirs();
                    }
                } else {
                    System.out.println("正在创建解压文件 - " + entryName);
                    String fileDir = path.substring(0, path.lastIndexOf("/"));
                    File fileDirFile = new File(fileDir);
                    if (!fileDirFile.exists()) {
                        fileDirFile.mkdirs();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(destFile + "/" + entryName));
                    bi = new BufferedInputStream(zipFile.getInputStream(ze2));
                    byte[] readContent = new byte[1024];
                    int readCount = bi.read(readContent);
                    while (readCount != -1) {
                        bos.write(readContent, 0, readCount);
                        readCount = bi.read(readContent);
                    }
                    bos.close();
                }
            }
            zipFile.close();

            //todo 下载成功
//            activity.mHandler.sendEmptyMessageDelayed(0,1000);

            activity.initTitle();
//            initTimer();
            activity.initVideo();
            activity.initSeekbar();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void commit(String uid, String id, String time, String token) {

        activity.progressbar();

        int timeParam = Integer.parseInt(time)/1000;

        final Call<SprotOverModule> sprotOverModuleCall = ApiHelper.sportOver(uid, id, String.valueOf(timeParam), token);
        sprotOverModuleCall.enqueue(new Callback<SprotOverModule>() {
            @Override
            public void onResponse(Call<SprotOverModule> call, Response<SprotOverModule> response) {
                SprotOverModule sprotOverModule = response.body();
                if (sprotOverModule == null) {
                    Log.d("ISportCourseTrainPresen", "sprotOverModule is null");
                    return;
                }
                if (sprotOverModule.getCode().equals("0")){
                    activity.mHandler.sendEmptyMessage(activity.FINISH);
                } else {
                    activity.mHandler.sendEmptyMessage(activity.FINISH);
                    Log.d("ISportCourseTrainPresen", "fail");
                }
            }

            @Override
            public void onFailure(Call<SprotOverModule> call, Throwable t) {
                activity.mHandler.sendEmptyMessage(activity.FINISH);
                Log.d("ISportCourseTrainPresen", "error");
            }
        });
    }
}
